package com.example.game.module.service;


import com.example.game.module.entity.Category;
import com.example.game.module.entity.Game;
import com.example.game.module.mapper.GameMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

@Service
public class GameService {

    @Autowired
    private GameMapper gameMapper;
    @Autowired
    private CategoryService categoryService;

    public int getTotalGames() {
        return gameMapper.getTotalRecode();
    }

    public List<Game> getAllGames(Integer page,Integer pageSize,String gameName) {

       int offset = (page - 1) * pageSize;
       return gameMapper.findAll(offset, pageSize,gameName);
    }

    public Game getById(BigInteger gameId) {

        Game game1 = gameMapper.getById(gameId);
        if (game1 == null ) {
            throw new RuntimeException("gameId对应记录不存在");
        }

        return gameMapper.getById(gameId);
    }

    public BigInteger edit(BigInteger gameId, String gameName, BigInteger playerNumber,
                           String title, String coverImages, String releaseDate, Float price,
                           BigInteger categoryId, String gameDescription, Float rating) {

        validateParameters(gameId, gameName, title, releaseDate, categoryId, gameDescription, price, rating);

        int timestamp = (int) (System.currentTimeMillis() / 1000);
        Game game = new Game();
        game.setGameName(gameName.trim());
        game.setPlayerNumber(playerNumber);
        game.setTitle(title.trim());
        game.setCategoryId(categoryId);
        game.setGameDescription(gameDescription);
        game.setRating(rating);
        game.setReleaseDate(releaseDate.trim());
        game.setPrice(price);
        game.setCoverImages(coverImages);

        if (gameId != null ) {
            // 更新操作
            game.setId(gameId);
            // 校验ID是否存在
            Game game1 = gameMapper.getById(gameId);
            if (game1 == null ) {
                throw new RuntimeException("gameId对应的记录不存在");
            }

            game.setUpdateTime(timestamp);
            gameMapper.update(game);
            return gameId; // 返回更新的游戏ID
        }   else {
            // 新增操作
            game.setCreateTime(timestamp);
            game.setUpdateTime(timestamp);
            game.setIsDeleted(0);
            gameMapper.insert(game);
            return game.getId(); // 返回新插入记录的ID
        }
    }

    private void validateParameters(BigInteger gameId, String gameName, String title,
                                    String releaseDate, BigInteger categoryId, String gameDescription, Float price, Float rating) {
        if (gameName == null || gameName.trim().isEmpty()) {
            throw new RuntimeException("游戏名称不能为空。");
        }
        if (title == null || title.trim().isEmpty()) {
            throw new RuntimeException("标题不能为空。");
        }
        if (releaseDate == null || releaseDate.trim().isEmpty()) {
            throw new RuntimeException("发布日期不能为空。");
        }
        if (categoryId == null ) {
            throw new RuntimeException("游戏类型Id不能为空");
        }
        // 检查categoryId是否存在
        Category category = categoryService.getById(categoryId);
        if (category == null) {
            throw new RuntimeException("游戏类型ID不存在");
        }
        if (gameDescription == null || gameDescription.trim().isEmpty()) {
            throw new RuntimeException("游戏简介不能为空");
        }
        if (price == null || price <= 0) {
            throw new RuntimeException("价格不能为空或不能为负数");
        }
        if (rating == null || rating <= 0) {
            throw new RuntimeException("评分不能为空或为负数");
        }
        // 如果gameId为null或小于等于0，并且是在更新操作中，则应该抛出异常
        // 但由于我们在edit方法中已经通过gameId判断了操作类型，这里可以不再校验gameId的具体值
    }

    /*

    public int  insert(String gameName, BigInteger playerNumber,String title,String coverImages,
                        String releaseDate, Float price, String gameType, String gameDescription,
                        Float rating) {

        int timestamp = (int) (System.currentTimeMillis() / 1000);
        Game game = new Game();
        game.setGameName(gameName.trim());
        game.setPlayerNumber(playerNumber);
        game.setTitle(title.trim());
        game.setGameType(gameType.trim());
        game.setGameDescription(gameDescription);
        game.setRating(rating);
        game.setReleaseDate(releaseDate.trim());
        game.setPrice(price);
        game.setGameType(gameType.trim());
        game.setCoverImages(coverImages);
        game.setCreateTime(timestamp);
        game.setUpdateTime(timestamp);
        game.setIsDeleted(0);
        return gameMapper.insert(game);
    }

    public int update( BigInteger gameId,String gameName,BigInteger playerNumber,
                           String title, String coverImages, String releaseDate, Float price,
                           String gameType,String gameDescription,Float rating) {
        int timestamp = (int) (System.currentTimeMillis() / 1000);

        Game game = new Game();
        game.setId(gameId);
        game.setGameName(gameName.trim());
        game.setPlayerNumber(playerNumber);
        game.setTitle(title.trim());
        game.setGameType(gameType.trim());
        game.setGameDescription(gameDescription);
        game.setRating(rating);
        game.setReleaseDate(releaseDate.trim());
        game.setPrice(price);
        game.setGameType(gameType.trim());
        game.setGameDescription(gameDescription);
        game.setCoverImages(coverImages);
        game.setUpdateTime(timestamp);
        return gameMapper.update(game);
    }

     */

    public int delete(BigInteger gameId) {

        return gameMapper.delete(gameId,(int)(System.currentTimeMillis() / 1000));
    }
    public Game extractById(BigInteger gameId) {

        return gameMapper.extractById(gameId);
    }
}
